import os
import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.select import Select
from win32com.client import Dispatch, DispatchEx
import win32com
import win32com.client
import web_operation
import word_operation
import json
class learning_crawlers:  
    
    #该爬虫针对学习进行时板块

    def first_observation(browser,Constant,tag_order):
        path_tag = "学习进行时板块" + Constant.now_date + " - " + Constant.last_date
        try:
            #点击第一观察TAG
            browser.execute_script("document.body.style.zoom='80%'")
            time.sleep(3)
            ob_url = browser.find_element(By.XPATH,"/html/body/div[3]/div[2]/div[1]/a["+ str(tag_order) +"]").get_attribute('href')
            browser.execute_script(f"window.open('{ob_url}','_blank');")
            time.sleep(2)
            web_operation.switch_to_first_table(browser)
            current_url = browser.current_url
            print(current_url)

            #第一观察有个focus   ?????????????????????有些图片，拼接的方法可能报错
            try:
                page_focus =  browser.find_element(By.XPATH,"/html/body/div[3]/div[3]/div/div/div/div[2]")#/html/body/div[3]/div[3]/div/div/div/div[2]
                focus_url =page_focus.find_element(By.TAG_NAME,"a").get_attribute('href')
                browser.execute_script(f"window.open('{focus_url}','_blank');")
                time.sleep(2)
                web_operation.switch_to_first_table(browser)
                try:
                    src = browser.find_element(By.XPATH,"/html/body/div[11]/div/div[1]/span/div/div").find_element(By.ID,"dhy-video").get_attribute("src")
                except:
                    src = " "
                print("视频地址：" + src)
                word_operation.main(focus_url,Constant.save_path + "\\" + path_tag ,src)
                print("文章记录完成")
                #关闭当前子窗口
                browser.close()
                #切换回“第一观察”
                web_operation.switch_to_first_table(browser)
            except Exception as e:
                print("点击"+browser.title+" -- focus页面失败")
                Constant.error = "进入"+browser.title+"-- focus页面失败 ---- " +  repr(e)
                print(Constant.error)
                browser.close()
                web_operation.switch_tabs_by_title('学习进行时-新华网',browser)
                return browser,Constant.error
                
            try:
                #进入文章列表
                page = browser.find_element(By.XPATH,"/html/body/div[3]/div[3]/div/div/div/div[3]")
                page_list =page.find_elements(By.TAG_NAME,"li")
                for i in range(0,len(page_list)): #因为文章是按照时间顺序排列的，找到第一个不符合时间的文档，就可以结束循环了。
                    date = browser.execute_script("return arguments[0].innerText;", browser.find_element(By.XPATH,'/html/body/div[3]/div[3]/div/div/div/div[3]/ul/li['+str(i+1)+']/div[1]/span'))
                    print(date)
                    if date in [Constant.now_date,Constant.last_date]: #符合爬取范围
                        page_list[i].find_element(By.CLASS_NAME,"list-tit").click()
                        # logs = browser.get_log('performance')
                        # image_urls = []
                        # for entry in logs:
                        #     data = json.loads(entry['message'])['message']
                        #     if 'Network.requestWillBeSent' in data['method']:
                        #         request = data['params']['request']
                        #         if request['resourceType'] == 'image':
                        #             image_urls.append(request['url'])
                        ##获取当前界面的url
                        web_operation.switch_to_first_table(browser)##切换至该文章界面
                        current_url = browser.current_url
                        print(current_url)
                        try:
                            src = browser.find_element(By.XPATH,"/html/body/div[11]/div/div[1]/span/div/div").find_element(By.ID,"dhy-video").get_attribute("src")
                        except:
                            src = " "
                        print("视频地址：" + src)
                        word_operation.main(current_url,Constant.save_path + "\\" + path_tag,src)
                        print("文章记录完成")
                        #关闭当前子窗口
                        browser.close()
                        #切换回“第一观察”
                        web_operation.switch_to_first_table(browser)
                        print(browser.title)
                        web_operation.switch_tabs_by_title(browser.title,browser)
                        # #关闭第一观察
                        # browser.close()
                        # web_operation.switch_tabs_by_title('学习进行时-新华网',browser)
                    else:
                        print(browser.title + "暂无更多符合" + Constant.now_date + " 和 " + Constant.last_date + "日期范围的报道")
                        #关闭第一观察
                        browser.close()
                        web_operation.switch_tabs_by_title('学习进行时-新华网',browser)
                        break
                return browser,Constant.error
            except Exception as e:
                print("点击"+browser.title+"获取文章失败")
                Constant.error = "进入"+browser.title+"获取文章失败 ---- " +  repr(e)
                print(Constant.error)
                browser.close()
                web_operation.switch_tabs_by_title('学习进行时-新华网',browser)
            return browser,Constant.error
        except Exception as e:
            print("点击"+browser.title+"失败")
            Constant.error = "进入"+browser.title+"失败 ---- " +  repr(e)
            print(Constant.error)
            web_operation.switch_tabs_by_title('学习进行时-新华网',browser)
            return browser,Constant.error

    def learning_area(browser,Constant,tag_order): ##针对学而时习之板块，下载视频
        path_tag = "学习进行时板块" + Constant.now_date + " - " + Constant.last_date
        try:
            #点击第一观察TAG
            browser.execute_script("document.body.style.zoom='80%'")
            time.sleep(3)
            ob_url = browser.find_element(By.XPATH,"/html/body/div[3]/div[2]/div[1]/a["+ tag_order +"]").get_attribute('href') #打开学而时习之板块
            browser.execute_script(f"window.open('{ob_url}','_blank');")
            time.sleep(2)
            web_operation.switch_to_first_table(browser)
            browser.execute_script("document.body.style.zoom='80%'")
            current_url = browser.current_url
            print(current_url)
                
            try:
                #进入文章列表
                page = browser.find_element(By.XPATH,"/html/body/div[3]/div[3]/div/div/div/div[2]")
                page_list =page.find_elements(By.TAG_NAME,"li")
                for i in range(0,len(page_list)): #因为文章是按照时间顺序排列的，找到第一个不符合时间的文档，就可以结束循环了。
                    date = browser.execute_script("return arguments[0].innerText;", browser.find_element(By.XPATH,'/html/body/div[3]/div[3]/div/div/div/div[2]/ul/li['+str(i+1)+']/div[2]/span'))
                    print(date)
                    if date in [Constant.now_date,Constant.last_date]: #符合爬取范围
                        page_url = page_list[i].find_element(By.XPATH,"/html/body/div[3]/div[3]/div/div/div/div[2]/ul/li["+str(i+1)+"]/div[2]/i/a").get_attribute('href')
                        browser.execute_script(f"window.open('{page_url}','_blank');")

                        # logs = browser.get_log('performance')
                        # image_urls = []
                        # for entry in logs:
                        #     data = json.loads(entry['message'])['message']
                        #     if 'Network.requestWillBeSent' in data['method']:
                        #         request = data['params']['request']
                        #         if request['resourceType'] == 'image':
                        #             image_urls.append(request['url'])
                        ##获取当前界面的url
                        web_operation.switch_to_first_table(browser)##切换至该文章界面
                        current_url = browser.current_url
                        print(current_url)
                        ##进入前，先定位到video标签，看看能不能拿到src的信息
                        try:
                            src = browser.find_element(By.XPATH,"/html/body/div[11]/div/div[1]/span/div/div").find_element(By.ID,"dhy-video").get_attribute("src")
                        except:
                            src = " "
                        print("视频地址：" + src)
                        word_operation.main(current_url,Constant.save_path + "\\" + path_tag,src)
                        print("文章/视频记录完成")
                        #关闭当前子窗口
                        browser.close()
                        #切换回“第一观察”
                        web_operation.switch_to_first_table(browser)
                        print(browser.title)
                        web_operation.switch_tabs_by_title(browser.title,browser)
                        # #关闭第一观察
                        # browser.close()
                        # web_operation.switch_tabs_by_title('学习进行时-新华网',browser)
                    else:
                        print(browser.title + "暂无更多符合" + Constant.now_date + " 和 " + Constant.last_date + "日期范围的报道")
                        #关闭第一观察
                        browser.close()
                        web_operation.switch_tabs_by_title('学习进行时-新华网',browser)
                        break

                return browser,Constant.error
            except Exception as e:
                print("点击"+browser.title+"获取文章失败")
                Constant.error = "进入"+browser.title+"获取文章失败 ---- " +  repr(e)
                print(Constant.error)
                browser.close()
                web_operation.switch_tabs_by_title('学习进行时-新华网',browser)
            return browser,Constant.error
        except Exception as e:
            print("点击"+browser.title+"失败")
            Constant.error = "进入"+browser.title+"失败 ---- " +  repr(e)
            print(Constant.error)
            web_operation.switch_tabs_by_title('学习进行时-新华网',browser)
            return browser,Constant.error


class politics_crawlers:
    ##该类针对时政联播页
    def politics(browser,Constant):
        path_tag = "学习联播板块" + Constant.now_date + " - " + Constant.last_date
        web_operation.switch_tabs_by_title("时政联播_新华时政_新华网",browser)
        key_words = ["习近平","总书记","新华时评","改革","党建","基层","机关","李强"]
        ##获取文章列表,检查日期
        date = Constant.now_date
        while date in [Constant.now_date,Constant.last_date]:
            page = browser.find_element(By.XPATH,"/html/body/div[4]/div/div[2]/div/div[1]")
            page_list = page.find_element(By.TAG_NAME,"div") ##发现所有文章
            #检查最后一篇文章是否在日期范围内，不在则不需要点击更多
            #date = browser.execute_script("return arguments[0].innerText;", browser.find_element(By.XPATH,'/html/body/div[3]/div[3]/div/div/div/div[3]/ul/li['+str(i+1)+']/div[1]/span'))
            date = page_list[-1].find_element(By.CLASS_NAME,"time").get_attribute("text")
            print(date)
            if date not in [Constant.now_date,Constant.last_date]:
                None
            else:
                browser.find_element(By.XPATH,"/html/body/div[4]/div/div[2]/div/div[2]").click()
                time.sleep(2)
        #检查符合日期的文章列表中，每一天文章是否存在关键词
        for p in page_list:
            page_title = p.find_element(By.CLASS_NAME,"tit").get_attribute("text")
            for key in key_words:
                if key in page_title:
                    #进入文章内
                    p.click()
                    web_operation.switch_to_first_table(browser)
                    current_url = browser.current_url
                    ##先判断文章中有没有视频
                    try:
                        src = browser.find_elements(By.ID,"dhy-video").get_attribute("src")
                    except:
                        src = " "
                        word_operation.main(current_url,Constant.save_path,src)
                        print("文章记录完成")
                        #关闭当前子窗口
                        browser.close()
                        #切换回“时政联播”
                        web_operation.switch_to_first_table(browser)
                        print(browser.title)
                        web_operation.switch_tabs_by_title(browser.title,browser)
                    break
                else:
                    continue
        print(browser.title + "暂无更多符合" + Constant.now_date + " 和 " + Constant.last_date + "日期范围的报道")
        #关闭时政联播
        browser.close()
        web_operation.switch_tabs_by_title('时政-新华网',browser)
        return browser        
            




def main(browser,Constant):
    for i in range(1,10): #学而时习之之前，可以用/html/body/div[3]/div[3]/div/div/div/div[3]，div[2]是focus页   后面几个涉及视频和结构有所区别，得重新写个函数
        browser,Constant.error = learning_crawlers.first_observation(browser,Constant,i)
    #进入学而时习之模块
    for i in range(10,13):
        browser,Constant.error = learning_crawlers.learning_area(browser,Constant,i)
    browser.close()
    web_operation.switch_tabs_by_title('时政-新华网',browser)


    return browser,Constant
